﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using ModelResourceManager.UserModel;
using ModelResourceManager.UserModel.Enums;
using WebMatrix.WebData;
using ResourceManagerWebApp.Models;
using System.Web.UI;

namespace ResourceManagerWebApp.Controllers
{
    public class AccountController : Controller
    {
        private UserManager _userManager;

        public AccountController()
        {
            _userManager = new UserManager();
        }

        [AllowAnonymous]
        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                var userManager = new UserManager();
                bool result = userManager.Add(new UserDTO()
                    {
                        UserId = Guid.NewGuid(),
                        FirstName = model.FirstName,
                        LastName = model.LastName,
                        Login = model.Login,
                        Password = model.Password,
                        Skype = model.Skype,
                        Email = model.Email,
                        Online = false,
                        MessageTimeSpan = model.MessageTimeSpan,
                        Role = UserRoles.User.ToString(),
                        Status = UserStatus.Active.ToString()
                    });
                if (result)
                    return RedirectToAction("Index", "Home");
            }
            return View(model);
        }

        [AllowAnonymous]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                Guid userId = _userManager.Login(model.Login, model.Password);
                if (userId != Guid.Empty)
                {
                    FormsAuthentication.SetAuthCookie(model.Login, true);
                    //Roles.AddUserToRole(model.Login, "User");
                    var idCookie = new HttpCookie("UserId", userId.ToString());
                    Response.Cookies.Add(idCookie);

                    return RedirectToAction("Index", "Home");
                }
            }

            return View(model);
        }

        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            HttpCookie idCookie = Request.Cookies["UserId"];
            if (idCookie != null && !string.IsNullOrEmpty(idCookie.Value))
            {
                var userId = Guid.Parse(idCookie.Value);
                _userManager.Logout(userId);
            }
            var userIdCookie = new HttpCookie("UserId", "");
            Response.Cookies.Add(userIdCookie);

            return RedirectToAction("Index", "Home");
        }

        [HttpGet]
        public ActionResult UserProfile()
        {
            HttpCookie idCookie = Request.Cookies["UserId"];
            if (idCookie != null && !string.IsNullOrEmpty(idCookie.Value))
            {
                var userId = Guid.Parse(idCookie.Value);
                UserDTO user = _userManager.GetCurrentUser(userId);
                var model = new ProfileModel()
                    {
                        Login = user.Login,
                        FirstName = user.FirstName,
                        LastName = user.LastName,
                        Email = user.Email,
                        MessageTimeSpan = user.MessageTimeSpan,
                        Skype = user.Skype
                    };
                return View(model);
            }
            return RedirectToAction("Index", "Home");
        }

        [HttpPost]
        public ActionResult UserProfile(ProfileModel model)
        {
            HttpCookie idCookie = Request.Cookies["UserId"];
            if (idCookie != null && !string.IsNullOrEmpty(idCookie.Value))
            {
                try
                {
                    Guid userId = Guid.Parse(idCookie.Value);
                    bool result = _userManager.Update(userId, new UserDTO()
                    {
                        Login = model.Login,
                        FirstName = model.FirstName,
                        LastName = model.LastName,
                        Email = model.Email,
                        MessageTimeSpan = model.MessageTimeSpan,
                        Skype = model.Skype
                    });
                    if (result)
                        return RedirectToAction("Index", "Home");
                }
                catch (Exception)
                {

                }
            }
            return View(model);
        }
    }

}
